package cn.com.api.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiT_member;
import cn.com.api.entity.ApiT_order;
import cn.com.api.entity.ApiT_order_return;
import cn.com.api.service.ApiT_memberService;
import cn.com.api.service.ApiT_orderService;
import cn.com.api.service.ApiT_order_returnService;
import cn.com.util.DateUtil;
import cn.com.util.JsonUtil;
import cn.com.util.Util;
import cn.com.util.Utils;

@Controller
@RequestMapping(Utils.SYS_API + "t_order_return/")
public class ApiT_order_returnController {
    @Autowired
    private ApiT_order_returnService apiT_order_returnService;
    @Autowired
    private ApiT_memberService memberService;
    @Autowired
	private ApiT_orderService apiT_orderService;
	
	private static Logger logger = Logger.getLogger(ApiT_order_returnController.class);
	
	/**
	 * 申请退款
	 * @author JW
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "insertT_order_return")
	public void insertT_order_return(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		//String id = request.getParameter("id");
		String user_id = request.getParameter("userId");
		String order_id = request.getParameter("orderNum");
		ApiT_order t_order = apiT_orderService.getOrderByOrderNum(order_id);
		//String create_date = request.getParameter("create_date");
		//String goods_id = request.getParameter("goods_id");
		//String product_id = request.getParameter("product_id");
		String return_money = request.getParameter("returnMoney");
		//String return_name = request.getParameter("returnName");
	    String return_reason = request.getParameter("returnReason");
	    String retrun_type=request.getParameter("returnType");
	    //if (!Util.isNull(return_reason)) {
			//return_reason=new String(return_reason.getBytes("ISO-8859-1"),"UTF-8");
		//}
		//String return_phone = request.getParameter("returnPhone");
		String img_1 = request.getParameter("img_1");
		String img_2 = request.getParameter("img_2");
		String img_3 = request.getParameter("img_3");
        
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			if(t_order.getMoney().compareTo(Util.getBigDecimal(return_money))>=0){
				ApiT_order_return t_order_return = new ApiT_order_return();
				t_order_return.setUser_id(user_id);
				t_order_return.setOrder_id(order_id);
				t_order_return.setStatus("0");
				t_order_return.setCreate_date(DateUtil.nowTimeToString());
				t_order_return.setReturn_money(Util.getBigDecimal(return_money));
				ApiT_member member= memberService.getMemberById(user_id);
				t_order_return.setReturn_phone(member.getC_mob());
				t_order_return.setReturn_name(member.getC_tname());
				t_order_return.setReturn_reason(return_reason);
			    t_order_return.setImg_1(img_1);
    		    t_order_return.setImg_2(img_2);
    		    t_order_return.setImg_3(img_3);
    		    t_order_return.setExt_1(retrun_type);
				apiT_order_returnService.insertT_order_return(t_order_return);
				apiT_orderService.updateOrderDDstatusByOrderNum(order_id, "6");
                map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "添加成功！");
			}else {
				map.put(Utils.RESULT, "0088");
				map.put(Utils.MESSAGE, "不能超出订单金额");
			}
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 根据id查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findT_order_returnById")
	public void findT_order_returnById(HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		Map<String,Object> map = new HashMap<String,Object>();
		try{
		    String id = request.getParameter("id");
		    ApiT_order_return t_order_return = apiT_order_returnService.findT_order_returnById(Integer.valueOf(id));
		
			// return
            map.put("datas", t_order_return);
            map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
        
	}
	
     /**
	 * 查询数据集
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findT_order_returns")
	public void findT_order_returns(HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		Map<String,Object> map = new HashMap<String,Object>();
		try{
		    List<ApiT_order_return> t_order_returns = apiT_order_returnService.findT_order_returns();
		
			// return
            map.put("datas", t_order_returns);
            map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
        
	}
	
    
	/**
	 * 修改
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@Transactional
	@RequestMapping(value = "updateT_order_return")
	public void updateT_order_return(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String id = request.getParameter("id");
		String user_id = request.getParameter("user_id");
		String order_id = request.getParameter("order_id");
		String create_date = request.getParameter("create_date");
		String goods_id = request.getParameter("goods_id");
		String product_id = request.getParameter("product_id");
		String return_money = request.getParameter("return_money");
		String return_name = request.getParameter("return_name");
		String return_reason = request.getParameter("return_reason");
		String return_phone = request.getParameter("return_phone");
		String reason = request.getParameter("reason");
		String duty = request.getParameter("duty");
		String money = request.getParameter("money");
		String check_time = request.getParameter("check_time");
		String img_1 = request.getParameter("img_1");
		String img_2 = request.getParameter("img_2");
		String img_3 = request.getParameter("img_3");
		String status = request.getParameter("status");
		String ext_1 = request.getParameter("ext_1");
		String ext_2 = request.getParameter("ext_2");
		String ext_3 = request.getParameter("ext_3");
		String ext_4 = request.getParameter("ext_4");
		String ext_5 = request.getParameter("ext_5");
        
        
		//String id = request.getParameter("id");
		Map<String,Object> map = new HashMap<String,Object>();
		
		try{
			if(id!=null && !"".equals(id.trim())){
				ApiT_order_return t_order_return = new ApiT_order_return();
				//获取原数据
				t_order_return  = apiT_order_returnService.findT_order_returnById(Integer.valueOf(id));
				//修改
				//t_order_return.setIs_open(Integer.valueOf(is_open));
				//
				int ret_i = apiT_order_returnService.updateT_order_return(t_order_return);
				
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "编辑成功！");
			}else{
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "检查失败！");
			}
		}
		catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
    
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "deleteT_order_return")
	public void deleteT_order_return(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
        String id = request.getParameter("id");
		Map<String,Object> map = new HashMap<String,Object>();
		
		try{
			if(id!=null && !"".equals(id.trim())){
	
				Integer ret_I = apiT_order_returnService.deleteT_order_return(Integer.parseInt(id));
				if(ret_I >0 ){
    				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
    				map.put(Utils.MESSAGE, "删除成功！");
                }else{
				    map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				    map.put(Utils.MESSAGE, "检查失败！");
			    }
			}
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
 
}


